package com.xltt.watchclient;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xltt.watchclient.model.ButtonInfo;
import com.xltt.watchclient.module.ScanManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.bluetooth.BluetoothDevice.BOND_BONDED;

public class MainActivity2 extends AppCompatActivity {

    private RecyclerView mScanRecyclerView;
    private QuickAdapter mScanQuickAdapter;
    private List<ButtonInfo> scanList = new ArrayList<>();
    private ScanManager mScanManager;
    private BluetoothDevice connectDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initViews();
    }

    private void initViews(){
        mScanRecyclerView = findViewById(R.id.scan_list);
        mScanQuickAdapter = new QuickAdapter(R.layout.item_button_view, scanList);
        mScanRecyclerView.setAdapter(mScanQuickAdapter);
        mScanQuickAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                ButtonInfo mButtonInfo = scanList.get(position);
                if (mButtonInfo != null) {
                    LogTool.d(mButtonInfo.name);
                    connectDevice = (BluetoothDevice) mButtonInfo.data;
                    if (connectDevice.getBondState() == BOND_BONDED) {
                    }
//                    if (nNavController.getPreviousBackStackEntry()!=null) {
//                        nNavController.getPreviousBackStackEntry()
//                                .getSavedStateHandle().set("device",connectDevice);
//                        LogTool.d("开始连接");
//                    }
//                    nNavController.popBackStack();
                }
            }
        });
        mScanManager = new ScanManager(this);
    }

    private void scanBle(){
        scanList.clear();

    }


    public void test(View view){
        if(view.getId() == R.id.test1){
            gotoNottion();
        }else if(view.getId() == R.id.test2){

        }else if(view.getId() == R.id.test3){

        }else if(view.getId() == R.id.test4){
            LogTool.d("点击了扫描");
            scanBle();
        }
    }

    private void gotoNottion(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            Intent intent = new Intent(
                    "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            startActivity(intent);
        } else {
            Toast.makeText(MainActivity2.this, "手机的系统不支持此功能", Toast.LENGTH_SHORT).show();
        }
    }



    class QuickAdapter extends BaseQuickAdapter<ButtonInfo, BaseViewHolder> {

        public QuickAdapter(int layoutResId, @Nullable List<ButtonInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ButtonInfo item) {
            helper.setText(R.id.item_title, item.name);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
